//
//  AnotherBarChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "AnotherBarChartViewController.h"


@interface AnotherBarChartViewController ()
@end

@implementation AnotherBarChartViewController

BarChartView *sampleAnotherBarChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initChartView{
    sampleAnotherBarChartView=[[BarChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleAnotherBarChartView.backgroundColor = [UIColor whiteColor];
    sampleAnotherBarChartView.delegate = self;
    [self.view addSubview:sampleAnotherBarChartView];
    [self initChartStyleAndData];
}


-(void)initChartStyleAndData{
    [self setupChartView:sampleAnotherBarChartView];
    [self setChartParam:sampleAnotherBarChartView];
    [self setLegendStyle:sampleAnotherBarChartView];
    [self initChartData];
    [sampleAnotherBarChartView animateWithXAxisDuration:2.0 yAxisDuration:2.0];
}

- (void)setupChartView:(BarChartView *)chartView
{
    chartView.descriptionText = @"descriptionText";
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    
    chartView.maxVisibleValueCount = 60;
    chartView.pinchZoomEnabled = NO;
    chartView.drawBarShadowEnabled = NO;
    chartView.drawGridBackgroundEnabled = NO;
}

-(void)setChartParam:(BarChartView *)chartView{
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.spaceBetweenLabels = 0.0;
    xAxis.drawGridLinesEnabled = NO;
    
    chartView.leftAxis.drawGridLinesEnabled = NO;
    chartView.rightAxis.drawGridLinesEnabled = NO;
}

//设置图例样式
-(void)setLegendStyle:(BarChartView *)chartView{
    chartView.legend.enabled = NO;
}

- (void)initChartData
{
    [self setDataCount:9 range:100 chart:sampleAnotherBarChartView];
}

- (void)setDataCount:(int)count range:(double)range chart:(BarChartView *)chartView
{
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        double mult = (range + 1);
        double val = (double) (arc4random_uniform(mult)) + mult / 3.0;
        [yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:[@((int)((BarChartDataEntry *)yVals[i]).value) stringValue]];
    }
    
    BarChartDataSet *set1 = nil;
    if (chartView.data.dataSetCount > 0)
    {
        set1 = (BarChartDataSet *)chartView.data.dataSets[0];
        set1.yVals = yVals;
        chartView.data.xValsObjc = xVals;
        [chartView notifyDataSetChanged];
    }
    else
    {
        set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"DataSet"];
        set1.colors = ChartColorTemplates.vordiplom;
        set1.drawValuesEnabled = NO;
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
        
        chartView.data = data;
    }
    
    [chartView setNeedsDisplay];
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}


@end